package homewoek12月22号.demo1.d5;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Demo1 {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
      while (true){
          System.out.println("【1】账号登录");
          System.out.println("【2】游客浏览");
          System.out.println("请输入:");
          int a = sc.nextInt();
          switch (a){
              case 1:
                  boolean login = login();
                  if (login==true){
                      return;
                  }
                  break;
              case 2:
                  otrherLogin();
                  break;
              default:
                  System.out.println("请输入正确浏览方式！");
          }
      }
    }
    private static boolean login() throws Exception {
        List<String> list = FileUtils.readLines(new File("src/com/czxy/demo1/d5/user.properties"));
        System.out.println("请输入用户名：");
        String str1 = sc.next();
        System.out.println("密码：");
        String str2 = sc.next();
        Date date = new Date();
        String ss = date.toLocaleString();
        for (String s : list) {
            String[] arr = s.split("=");
            if (str1.equals(arr[0])&&str2.equals(arr[1])){
                System.out.println("登录成功！");
                FileUtils.writeStringToFile(new File("src/com/czxy/demo1/d5//data.txt"),str1+ss+"登录成功！"+"\n",true);
                return true;
            }
        }
        System.out.println("登录失败！");
        FileUtils.writeStringToFile(new File("src/com/czxy/demo1/d5//data.txt"),str1+ss+"登录失败！"+"\n",true);
        return false;
    }
    private static void otrherLogin() throws IOException {
        FileUtils.writeStringToFile(new File("src/com/czxy/demo1/d5//data.txt"),"游客"+(new Date().toLocaleString())+"进行浏览！"+"\n",true);
        List<String> list = FileUtils.readLines(new File("src/com/czxy/demo1/d5/data.txt"));
        System.out.println(list.get(list.size()-1));
    }
}
